package com.ican.dao.rule;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.springframework.stereotype.Repository;

import com.ican.common.hibernate.HibernateDao;
import com.ican.common.hibernate.Page;
import com.ican.domain.rule.WebsiteRule;

@Repository("websiteRuleDao")
public class WebsiteRuleDao extends HibernateDao<WebsiteRule, Integer> {

    public WebsiteRule findWebsiteById(Integer id){
        return this.get(id);
    }
    
    public Page<WebsiteRule> findAllWebsite(Page<WebsiteRule> page, WebsiteRule webSite){
        List<String> params = new ArrayList<String>();
        if(webSite != null){
            if(webSite.getName() != null && webSite.getName().length() > 0){
                params.add("name like '%" + webSite.getName() + "%'");
            }
            if(webSite.getUrl() != null && webSite.getUrl().length() > 0){
                params.add("url like '%" + webSite.getUrl() + "%'");
            }
            if(webSite.getDataStatus() == 0 || webSite.getDataStatus() == 1){
                params.add("dataStatus = " + webSite.getDataStatus());
            }
        }
        StringBuffer sb = new StringBuffer("from WebsiteRule");
        if(params.size() > 0){
            sb.append(" where ");
            for(int i = 0; i < params.size(); i++){
                if(i != 0){
                    sb.append(" and ");
                }
                sb.append(params.get(i));
            }
        }
        sb.append(" order by createTime desc ");
        return this.findPage(page, sb.toString());
    }
    
    public List<WebsiteRule> findAllWebSite(WebsiteRule webSite){
        List<String> params = new ArrayList<String>();
        if(webSite != null){
            if(webSite.getName() != null && webSite.getName().length() > 0){
                params.add("name like '%" + webSite.getName() + "%'");
            }
            if(webSite.getUrl() != null && webSite.getUrl().length() > 0){
                params.add("url like '%" + webSite.getUrl() + "%'");
            }
            if(webSite.getDataStatus() == 0 || webSite.getDataStatus() == 1){
                params.add("dataStatus = " + webSite.getDataStatus());
            }
        }
        StringBuffer sb = new StringBuffer("from WebsiteRule");
        if(params.size() > 0){
            sb.append(" where ");
            for(int i = 0; i < params.size(); i++){
                if(i != 0){
                    sb.append(" and ");
                }
                sb.append(params.get(i));
            }
        }
        sb.append(" order by createTime desc ");
        return this.find(sb.toString());
    }
    
    public void addWebSite(WebsiteRule webSite){
        webSite.setCreateTime(new Date());
        webSite.setModifyTime(new Date());
        this.save(webSite);
    }
    
    public void modifyWebSite(WebsiteRule webSite){
        if(webSite == null || webSite.getId() == null){
            return;
        }
        WebsiteRule entity = this.get(webSite.getId());
        if(entity == null){
            return;
        }
        if(webSite.getDataStatus() == 0 || webSite.getDataStatus() == 1){
            entity.setDataStatus(webSite.getDataStatus());
        }
        if(webSite.getName() != null){
            entity.setName(webSite.getName());
        }
        if(webSite.getDescription() != null){
            entity.setDescription(webSite.getDescription());
        }
        if(webSite.getUrl() != null){
            entity.setUrl(webSite.getUrl());
        }
        entity.setModifyTime(new Date());
        this.save(entity);
    }
    
    public void updateWebsiteScanNum(){
        String sql = "{call p_update_website_category_scan_num()}";
        this.createSqlQuery(sql).executeUpdate();
    }
}
